export default {
  data() {
    return {
      isShow: 1,
      displa: '',
      allList: {
			  brandId: 0,
			  brandName: '',
			  xlId: 0,
			  xlName: '',
			  pzId: 0,
			  pzName: '',
			  factoryprice: '',
			  color: ''
      },

      specilist: [],
      // 品牌
      historylistpp: '', //品牌历史记录
      scrollTop: 0,
      Brandlist: [],
      indexlist: [],
      addtoView: '', //定位abc
      brand: '',
      //系列
      bid: '',
      serlsit: '',
      Serieslist: [], //系列
      //排量
      pid: '',
      prands: '',
      displacement: [], //排量
      //年款
      yid: '',
      years: '',
      modelyear: [], //年款

      // 车子
      Seriesid: null,
      visible: true, //picker-view属性配置时间选择器
      indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`, //设置选择器中间选中框的样式
      Configurationlist: [], //配置list-页面渲染显示
      Configuname: '', //配置name-页面渲染显示
      typeid: '120', //传入的值，不同的页面传入不同的数字，页面显示不同的区域
      selectShow: 1, //控制列表显示隐藏 1品牌，2系列，3排量，4年款，5车子
      CarsaddtoView: '', //车子定位
      serid: 0, //系列id,
      pageFrom: 'specifAll' // 来自页面
    }
  },
  onLoad(typeid) {
    if(typeid.isShow){
      this.isShow = Number(typeid.isShow)
    }
    //跳到有规格的模块传入110，直接跳到品牌模块传入120
    this.typeid = typeid.type
    if (this.typeid === '120') {
      this.selectShow = 1
    } else {
      this.selectShow = 1
    }
    this.pageFrom = typeid.pageFrom || 'specifAll'
  },
  // 监听返回
  onBackPress(e) {
    const pageOut = {
      '110': 1,
      '120': 1
    }
    if (e.from === 'navigateBack') {
      return false //监听返回onBackPress一定要return
    }
    if (this.selectShow !== pageOut[this.typeid]) {
      this.selectShow -= 1
      return true
    } else {
      return false
    }
  },
  // vue实例加载完成
  created() {
    // this.brandinterface() //品牌系列配置
    this.GetCarBrandList()
    this.GetHotCarBrandList()
  },
  methods: {
    //退回
    delcar(index) {
		  this.selectShow = index
    },
    // 品牌接口请求
    async GetCarBrandList() {
      try {
        const res = await this.$api.common.GetCarBrandList()
        this.Brandlist = res.data
        this.Brandlist.forEach((item, index) => {
          this.indexlist.push(item.initial)
        })
      } catch (err) {
        console.log(err)
      }
    },
    // 热门品牌
    async GetHotCarBrandList() {
      try {
        const res = await this.$api.common.GetHotCarBrandList({
          type: 0
        })
        this.historylistpp = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 品牌
    // 楼层定位
    addressToView(event) {
      var id = event.currentTarget.dataset.id
      this.addtoView = id
    },
    //选择品牌
    searchBrand(type, index) {
      // console.log(type,index,'选择品牌')

      let brands = {}
      if (index === 0) {
        this.allList.brandId = type.brandId
        this.allList.brandName = type.branName
        brands = {
          brand: type.branName,
          bid: type.brandId
        }
      } else {
        this.allList.brandId = type.bid
        this.allList.brandName = type.brand
        brands = {
          brand: type.brand,
          bid: type.bid
        }
      }
      // 存储品牌
      uni.$emit(this.pageFrom, {
        brandlist: brands
      })
      // this.$store.commit('setBrand', brands)       ??????
      //页面显示系列名称/id
      this.brand = brands.brand
      this.bid = brands.bid
      this.selectShow = 2
      //系列
      this.GetCarTypeLists()
    },
    // 根据品牌id获得分组系列
    async GetCarTypeLists() {
      try {
        let data = {
          bid: this.bid
        }
        const res = await this.$api.common.GetCarTypeLists(data)
        this.Serieslist = res.data
      } catch (err) {
        console.log(err)
      }
    },
    //根据系列id获取所有排量分组
    async GetcarTypeDisplacementLists(typesid) {
      try {
        let data = {
          carTypeid: typesid
        }
        const res = await this.$api.common.GetcarTypeDisplacementLists(data)
        this.displacement = res.data
      } catch (err) {
        console.log(err)
      }
    },
    //根据系列id以及排量获取车子
    async GetCarList(typeid) {
      try {
        let data = {
          carTypeid: this.serid,
          displacement: typeid
        }
        const res = await this.$api.common.GetCarList(data)
        this.modelyear = res.data
      } catch (err) {
        console.log(err)
      }
    },
    //选择了系列
    seriesbtn(type) {
      // console.log(type,'系列')
      this.allList.xlId = type.carTypeId
      this.allList.xlName = type.carTypeName
      //存储系列
      let serJuu = {
        carTypeId: type.carTypeId,
        carTypeName: type.carTypeName
      }
      //系列赋值
      uni.$emit(this.pageFrom, {
        Serieslist: serJuu
      })
      // this.$store.commit('setSeries', serJuu)
      this.serlsit = type.carTypeName
      this.serid = type.carTypeId //系列id
      this.selectShow = 3
      this.GetcarTypeDisplacementLists(type.carTypeId)
    },
    //选择了排量
    sdisplacementbtn(type) {
      // console.log(type,'排量')
      let carTypeId = type.carTypeId
      this.displa = type.displacement
      //存储排量
      if (type.displacement === '-1') {
        this.prands = '电动/混动'
      } else {
        this.prands = type.displacement
      }
      let displac = {
        pid: type.carTypeId,
        pName: type.displacement
      }
      //排量赋值
      uni.$emit(this.pageFrom, {
        Displacement: displac
      })
      this.selectShow = 4
      this.GetCarList(type.displacement)
    },
    //年款
    modelyearbtn(type) {
      // console.log(type,'年款')
      //存储年款
      let displac = {
        years: type.data
      }
      //年款赋值
      uni.$emit(this.pageFrom, {
        Modelyear: displac
      })
      this.years = type.data
      this.selectShow = 5
      this.Configurationlist = type.list
    },
    // 新增车子
    async addCarFindPrice() {
		  try {
		    let data = {
		      'brand_Id': String(this.allList.brandId),
		      'brand_Name': this.allList.brandName,
		      'car_Type_Id': String(this.allList.xlId),
		      'car_Type_Name': this.allList.xlName,
		      'configuration_Id': String(this.allList.pzId),
		      'configuration_Name': this.allList.pzName+this.displa,
		      'colour': this.allList.color,
		      'car_Price': this.allList.factoryprice || 0
		    }
		    const res = await this.$api.comparison.addCarFindPrice(data)
        console.log(res)
		    if(res.code === 200){
		      this.$refs.uToast.show({
		    	  title: '新增成功',
		    	  type: 'success',
		      })
		    }else{
		      this.$refs.uToast.show({
		    	  title: res.msg,
		    	  type: 'warning',
		      })
		    }
		    this.list = res.data.list
		  } catch (e) {

		  }
    },
    // 车子
    //除询比价
    async addCar(type) {
		  console.log(type,'配置')
		  //车子name
		  let factoryprice = type.factoryprice.replace('万','')
		  let fact = factoryprice*10000
		  //列表进入
		  this.toconfigureid = type.id
		  if (this.typeid === '120') {
		    uni.$emit(this.pageFrom, {
		      Configulist: type,
		      pageFrom: this.pageFrom
		    })
		    setTimeout(() => {
		      uni.navigateBack()
		    },1000)

		  } else {
		    uni.$emit(this.pageFrom, {
		      Configulists: type,
		      pageFrom: this.pageFrom
		    })
		    this.selectShow = 1
		    uni.navigateBack()
		  }

    },
    //添加询比价
    async Configbtn(type) {
      console.log(type,'配置')
      //车子name
      let factoryprice = type.factoryprice.replace('万','')
      let fact = factoryprice*10000
      this.allList.factoryprice = fact
      this.allList.pzId = type.id
      this.allList.pzName = type.name
      //列表进入
      this.toconfigureid = type.id
      try {
			  let data = {
			    'Id': type.id
			  }
			  const res = await this.$api.common.GetCarColourList(data)
			  let a = res.data.inColour
			  let b = res.data.outerColour
        console.log(res)
			  this.allList.color = JSON.stringify(res.data)

			  this.addCarFindPrice()
      } catch (e) {

      }
      if (this.typeid === '120') {
        uni.$emit(this.pageFrom, {
          Configulist: type,
          pageFrom: this.pageFrom
        })
        setTimeout(() => {
          uni.navigateBack()
        },1000)

      } else {
        uni.$emit(this.pageFrom, {
          Configulists: type,
          pageFrom: this.pageFrom
        })
        this.selectShow = 1
        uni.navigateBack()
      }

    }

  }
}
